package com.wink_172.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.wink_172.commons.R;
import com.wink_172.library.utils.DisplayUtil;

import org.xutils.x;

/**
 *
 * 图片裁剪之扇形图
 * https://blog.csdn.net/u013085697/article/details/52096703
 * https://www.jb51.net/article/112133.htm
 *圆弧  扇形
 * */
public class CircleImageView extends View {

    private Paint mPaint;
    private Paint mTargetPaint;
    private Bitmap mSourceBitmap;
    private Bitmap mTargetBitmap;
    private Bitmap mBitmap3;
    private Canvas mCanvas;

    private int mWidth;
    private int mHeight;

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(getResources().getColor(R.color.yellow02));
        mTargetPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        setBackgroundColor(getResources().getColor(R.color.purple_200));

//        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        mSourceBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_defalut);
        mTargetBitmap = Bitmap.createBitmap(mSourceBitmap.getWidth(), mSourceBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        mBitmap3 = BitmapFactory.decodeResource(getResources(), R.mipmap.grade_sss);
        mCanvas = new Canvas(mTargetBitmap);

        mWidth = mSourceBitmap.getWidth();
        mHeight = mSourceBitmap.getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect src = new Rect();// 图片
        Rect dst = new Rect();// 屏幕位置及尺寸

        //src 这个是表示绘画图片的大小
//        src.left = 0;   //0,0
//        src.top = 0;
//        src.right = mSourceBitmap.getWidth();// mBitDestTop.getWidth();,这个是桌面图的宽度，
//        src.bottom = mSourceBitmap.getHeight();//mBitDestTop.getHeight()/2;// 这个是桌面图的高度的一半
//        // 下面的 dst 是表示 绘画这个图片的位置
//        dst.left = test;    //miDTX,//这个是可以改变的，也就是绘图的起点X位置
//        dst.top = 0;    //mBitQQ.getHeight();//这个是QQ图片的高度。 也就相当于 桌面图片绘画起点的Y坐标
//        dst.right = DisplayUtil.dp2px(x.app(),56);    //miDTX + mBitDestTop.getWidth();// 表示需绘画的图片的右上角
//        dst.bottom =  DisplayUtil.dp2px(x.app(),56f);    // mBitQQ.getHeight() + mBitDestTop.getHeight();//表示需绘画的图片的右下角

        mTargetPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        // 再绘制Bitmap
        canvas.drawBitmap(mBitmap3, 40, 0, null);

        //先绘制的是dst，后绘制的是src
        RectF rectF = new RectF(0, 0, mWidth, mHeight);

        mCanvas.drawArc(rectF, 90, 270, true, mPaint);
        ////////////////////////////////////////////
        // 再绘制Bitmap
        mCanvas.drawBitmap(mSourceBitmap, 0, 0, mTargetPaint);


        canvas.drawBitmap(mTargetBitmap, 40, 0, null);//这个才是最重要的bitmap
        //////////////////////////////////

    }
}